<template>
  <span style="color: #ff5454">{{ time }}</span>
</template>

<script>
export default {
  props: {
    endDate: {
      type: Number
    }
  },
  data() {
    return {
      time: '',
      flag: false
    }
  },
  mounted() {
    const time = setInterval(() => {
      if (this.flag == true) {
        clearInterval(time)
      }
      this.timeDown()
    }, 500)
  },
  methods: {
    timeDown() {
      const endDate = this.endDate
      const nowTime = new Date()
      const leftTime = parseInt((endDate - nowTime.getTime()) / 1000)
      const d = parseInt(leftTime / (24 * 60 * 60))
      const h = this.formate(parseInt((leftTime / (60 * 60)) % 24))
      const m = this.formate(parseInt((leftTime / 60) % 60))
      const s = this.formate(parseInt(leftTime % 60))
      if (leftTime <= 0) {
        this.flag = true
        this.$emit('time-end')
      }
      this.time = `${m}分${s}秒`
    },
    formate(time) {
      if (time <= 0) {
        return '00'
      } else if (time >= 10) {
        return time
      } else {
        return `0${time}`
      }
    }
  }
}
</script>

<style scoped>
</style>
